/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-03-14     DRZZZ       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <stdio.h>
#include "board.h"
#include "relay_management.h"
#include "connect_8266.h"
#include "connect_layer.h"
#include "head.h"

#define LOG_TAG        "ESP8266"
#define LOG_LVL        LOG_LVL_INFO
#include <ulog.h>

#define THREAD_PRIORITY         5
#define THREAD_TIMESLICE        2
#define C8266_UART       "uart3"

ALIGN(RT_ALIGN_SIZE)
static char C8266_stack[1024];
struct rt_thread C8266_RT;
/* 用于接收消息的信号量 */
static struct rt_semaphore rx_sem;
rt_device_t C8266_serial;
static char str[10] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    rt_sem_release(&rx_sem);
    return RT_EOK;
}


static void C8266_entry(void *parameter)
{
    char ch = 0;
    while (1)
    {
        while (rt_device_read(C8266_serial, -1, &ch, 1) != 1)
        {
            rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
        }
        LOG_I("Receive from 8266!");
        printf("%c\n",ch);
        switch (ch)
        {
            case 0x30:Set_Peripheral(RELAY_PIN_1, WORK);break;// 0 relay_1_flag = 1;
            case 0x31:Set_Peripheral(RELAY_PIN_1, STOP);break;// 1 relay_1_flag = 0;
            case 0x32:Set_Peripheral(RELAY_PIN_2, WORK);break;// 2 relay_2_flag = 1;
            case 0x33:Set_Peripheral(RELAY_PIN_2, STOP);break;// 3 relay_2_flag = 0;
            case 0x34:Set_Peripheral(RELAY_PIN_3, WORK);break;// 4 relay_3_flag = 1;
            case 0x35:Set_Peripheral(RELAY_PIN_3, STOP);break;// 5 relay_3_flag = 0;
            case 0x36:Set_Peripheral(RELAY_PIN_4, WORK);break;// 6
            case 0x37:Set_Peripheral(RELAY_PIN_4, STOP);break;// 7
            case 0x38:Set_Peripheral(RELAY_PIN_5, WORK);break;// 8
            case 0x39:Set_Peripheral(RELAY_PIN_5, STOP);break;// 9
            case 0x40:Set_Peripheral(RELAY_PIN_6, WORK);break;// @
            case 0x41:Set_Peripheral(RELAY_PIN_6, STOP);break;// A
            case 0x42:Set_Peripheral(RELAY_PIN_7, WORK);break;// B
            case 0x43:Set_Peripheral(RELAY_PIN_7, STOP);break;// C
            case 0x44:Set_Peripheral(RELAY_PIN_8, WORK);break;// D
            case 0x45:Set_Peripheral(RELAY_PIN_8, STOP);break;// E
#if (CODE == FIRST)
            case 0x46:rt_device_write(Board_serial, 0, &str[0], 1);break;// F
            case 0x47:rt_device_write(Board_serial, 0, &str[1], 1);break;// G
            case 0x48:rt_device_write(Board_serial, 0, &str[2], 1);break;// H
            case 0x49:rt_device_write(Board_serial, 0, &str[3], 1);break;// I
            case 0x4A:rt_device_write(Board_serial, 0, &str[4], 1);break;// J
            case 0x4B:rt_device_write(Board_serial, 0, &str[5], 1);break;// K
            case 0x4C:rt_device_write(Board_serial, 0, &str[6], 1);break;// L
            case 0x4D:rt_device_write(Board_serial, 0, &str[7], 1);break;// M
            case 0x4E:rt_device_write(Board_serial, 0, &str[8], 1);break;// N
            case 0x4F:rt_device_write(Board_serial, 0, &str[9], 1);break;// O
#endif
        }
        rt_thread_mdelay(200);
    }
}

static int C8266_thread()
{
    /* 查找系统中的串口设备 */
    C8266_serial = rt_device_find(C8266_UART);

    if (!C8266_serial)
    {
        rt_kprintf("find %s failed!\n", C8266_UART);
        return RT_ERROR;
    }

    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;  /* 初始化配置参数 */

    config.baud_rate = BAUD_RATE_115200;        // 修改波特率为 115200
    config.data_bits = DATA_BITS_8;             // 数据位 8
    config.stop_bits = STOP_BITS_1;             // 停止位 1
    config.bufsz     = 128;                     // 修改缓冲区 buff size 为 128
    config.parity    = PARITY_NONE;             // 无奇偶校验位

    rt_device_control(C8266_serial, RT_DEVICE_CTRL_CONFIG, &config);

    /* 初始化信号量 */
    rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
    /* 以中断接收及轮询发送模式打开串口设备 */
    rt_device_open(C8266_serial, RT_DEVICE_FLAG_INT_RX);
    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(C8266_serial, uart_input);
    /* 创建线程1，名称是trol_RTT，入口是control_entry*/
    rt_thread_init(&C8266_RT,
                   "8266_RT",
                   C8266_entry,
                   RT_NULL,
                   &C8266_stack[0],
                   sizeof(C8266_stack),
                   THREAD_PRIORITY, THREAD_TIMESLICE);
    rt_thread_startup(&C8266_RT);
    LOG_I("--ESP8266 init over!");
    return 0;
}

INIT_DEVICE_EXPORT(C8266_thread);
